// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Eggman Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 71 : TYPE_EGGMAN

// Function declarations
reserve function Eggman_Function163
reserve function Eggman_Function164

// Static Values

// Tables
public table Eggman_table36
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0
end table


function Eggman_Function163
	Sin256(temp0, object.value3)
	temp0 <<= 10
	object.ypos += temp0
	object.value3 += 2
	object.value3 &= 255
end function


function Eggman_Function164
	object.value0 += object.xvel
	object.value1 += object.yvel
	object.xpos = object.value0
	object.ypos = object.value1
end function


event ObjectMain
	switch object.state
	case 0
		GetTableValue(temp0, 0, Eggman_table36)
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object[currentPlayer].xpos >= temp0
				object.xvel = 0x6000
				object.yvel = -0x18000
				object.value1 = object.ypos
				object.value3 = 0
				object.state++
				object.priority = PRIORITY_ACTIVE
				ResetObjectEntity(26, TypeName[Music Event], 0, 0, 0)
				object[26].priority = PRIORITY_ACTIVE
			end if
		next
		CallFunction(Eggman_Function164)
		if object.state == 0
			CallFunction(Eggman_Function163)
		end if
		break
	case 1
		temp1 = -2
		GetTableValue(temp0, 1, Eggman_table36)
		if object.value0 >= temp0
			object.value0 = temp0
			object.xvel = 0
			temp1++
		end if
		GetTableValue(temp0, 2, Eggman_table36)
		if object.value1 <= temp0
			object.value1 = temp0
			object.yvel = 0
			temp1++
		end if
		if temp1 == 0
			object.xvel = 0x14000
			object.yvel = -0x20000
			object.state++
		end if
		CallFunction(Eggman_Function164)
		break
	case 2
		temp1 = -2
		GetTableValue(temp0, 3, Eggman_table36)
		if object.value0 >= temp0
			object.value0 = temp0
			object.xvel = 0
			temp1++
		end if
		GetTableValue(temp0, 4, Eggman_table36)
		if object.value1 <= temp0
			object.value1 = temp0
			object.yvel = 0
			temp1++
		end if
		if temp1 == 0
			object.yvel = -0x18000
			object.state++
		end if
		CallFunction(Eggman_Function164)
		break
	case 3
		GetTableValue(temp0, 5, Eggman_table36)
		if object.value1 <= temp0
			object.value1 = temp0
			if object.value2 == 0
				object.xvel = 0x14000
				object.yvel--
			else
				object.xvel = 0x28000
				object.yvel--
			end if
			object.direction = FLIP_X
			object.value0 = object.xpos
			object.state++
			CallFunction(Eggman_Function164)
		else
			object.value4 += 2
			object.value4 &= 255
			Cos256(temp0, object.value4)
			if temp0 > 0
				object.direction = FLIP_X
			else
				object.direction = FLIP_NONE
			end if
			Sin256(object.xpos, object.value4)
			object.xpos <<= 12
			object.xpos += object.value0
			temp0 = object.yvel
			temp1 = 0x7FFFFFFF
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				if object[currentPlayer].ypos < temp1
					temp1 = object[currentPlayer].ypos
				end if
			next
			temp1 -= object.value1
			if temp1 >= 0
				temp1 -= 0x480000
				if temp1 >= 0
					temp0 >>= 1
					temp1 -= 0x280000
					if temp1 >= 0
						temp0 >>= 1
						temp1 -= 0x280000
						if temp1 >= 0
							temp0 = 0
						end if
					end if
				end if
			end if
			if object.value2 == 1
				temp0 <<= 1
			end if
			if temp0 == 0
				object.ypos = object.value1
				CallFunction(Eggman_Function163)
			else
				object.value1 = object.ypos
				object.value3 = 0
				object.value1 += temp0
				object.ypos = object.value1
			end if
		end if
		break
	case 4
		temp1 = -2
		GetTableValue(temp0, 6, Eggman_table36)
		if object.value0 >= temp0
			object.value0 = temp0
			object.xvel = 0
			temp1++
		end if
		GetTableValue(temp0, 7, Eggman_table36)
		if object.value1 <= temp0
			object.value1 = temp0
			object.yvel = 0
			temp1++
		end if
		if temp1 == 0
			if object.value2 == 0
				object.direction = FLIP_NONE
			end if
			object.value3 = 0
			object.state++
		end if
		CallFunction(Eggman_Function164)
		break
	case 5
		temp2 = 0
		if object.value2 == 0
			GetTableValue(temp0, 8, Eggman_table36)
			GetTableValue(temp1, 9, Eggman_table36)
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				if object[currentPlayer].xpos >= temp0
					if object[currentPlayer].ypos <= temp1
						temp2 = 1
						object.value5 = 50
						object.animation = 4
						object.animationTimer = 32
					end if
				end if
			next
		else
			temp2 = 1
			object.value5 = 1
		end if
		if temp2 == 1
			ResetObjectEntity(26, TypeName[Music Event], 1, 0, 0)
			object[26].priority = PRIORITY_ACTIVE
			object.state++
		end if
		CallFunction(Eggman_Function164)
		CallFunction(Eggman_Function163)
		break
	case 6
		object.value5--
		if object.value5 == 0
			object.value5 = 0
			object.xvel = 0x40000
			object.yvel = -0x4000
			object.priority = PRIORITY_ACTIVE
			object.animation = 6
			object.animationTimer = 0
			object.value6 = 2
			object.value1 = object.ypos
			object.state++
			if stage.debugMode == 0
				if object.value10 == 0
					achievement.trigger = 7
					//CallNativeFunction2(SetAchievement, 6, 100)
				end if
			end if
		else
			if object.value5 == 24
				object.animation = 1
				object.animationTimer = 0
				object.direction = FLIP_X
			end if
		end if
		CallFunction(Eggman_Function164)
		if object.state == 6
			CallFunction(Eggman_Function163)
		end if
		break
	case 7
		if object.outOfBounds == 1
			ResetObjectEntity(object.entityPos, TypeName[Blank Object], 0, 0, 0)
			object.value2 = 0
		else
			CallFunction(Eggman_Function164)
		end if
		break
	end switch
	if object.value8 != 0
		if object.value9 > 0
			object.value9--
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.value9 == 0
				CheckEqual(object[currentPlayer].animation, ANI_HURT)
				temp0 = checkResult
				CheckEqual(object[currentPlayer].animation, ANI_DYING)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].animation, ANI_DROWNING)
				temp0 |= checkResult
				if temp0 != 0
					if object.animation != 2
						object.animation = 2
						object.animationTimer = 0
					end if
					if currentPlayer == 0
						object.value10 = 1
					end if
				end if
				BoxCollisionTest(C_TOUCH, object.entityPos, -23, -16, 23, 16, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
				if checkResult == 0
					BoxCollisionTest(C_TOUCH, object.entityPos, -16, -21, 16, 21, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
				end if	
				if object.state == 7
					checkResult = 0
				else
					if checkResult == 1
						CallFunction(PlayerObject_CheckHit)
					else
						CallFunction(BossTarget)
					end if
				end if
				if checkResult == 1
					object.value8--
					if object.value8 == 0
						player.score += 1000
						object.animation = 4
						object.animationTimer = 0
						object.value2 = 1
					else
						object.animation = 3
						object.animationTimer = 0
						object.value9 = 32
						PlaySfx(SfxName[Boss Hit], 0)
					end if
				end if
			end if
		next
		foreach (TypeName[Bullet], arrayPos1, ACTIVE_ENTITIES)
			if object.value9 == 0
				BoxCollisionTest(C_TOUCH, object.entityPos, -23, -16, 23, 16, arrayPos1, -6, -4, 6, 4)	
				if checkResult == 0
					BoxCollisionTest(C_TOUCH, object.entityPos, -16, -21, 16, 21, arrayPos1, -6, -4, 6, 4)	
				end if	
				if object.state == 7
					checkResult = 0
				end if
				if checkResult == 1
					object.value8--
					if object.value8 == 0
						player.score += 1000
						object.animation = 4
						object.animationTimer = 0
						object.value2 = 1
					else
						object.animation = 3
						object.animationTimer = 0
						object.value9 = 32
						PlaySfx(SfxName[Boss Hit], 0)
					end if
				end if
			end if
		next		
	end if
	if object.value2 == 1
		temp0 = oscillation
		temp0 &= 7
		if temp0 == 0
			Rand(temp0, 96)
			temp0 -= 48
			temp0 <<= 16
			temp0 += object.xpos
			Rand(temp1, 48)
			temp1 -= 24
			temp1 <<= 16
			temp1 += object.ypos
			CreateTempObject(TypeName[Explosion], 0, temp0, temp1)
			PlaySfx(SfxName[Explosion], 0)
		end if
	end if
end event


event ObjectDraw
	switch object.animation
	case 1
		temp0 = object.animationTimer
		temp0 /= 6
		object.animationTimer++
		object.animationTimer %= 12
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 2
		temp0 = object.animationTimer
		temp0 %= 10
		temp0 /= 5
		temp0 += 9
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			if object[0].animation != ANI_DYING
				object.animation = 1
			end if
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 3
		temp0 = object.animationTimer
		temp0 &= 1
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 30
			object.animationTimer = 0
			object.animation = 1
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 4
		temp0 = object.animationTimer
		temp0 >>= 5
		temp0 += 4
		object.animationTimer++
		object.animationTimer &= 63
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 5
		DrawSpriteFX(6, FX_FLIP, object.xpos, object.ypos)
		break
	case 6
		temp0 = object.animationTimer
		temp0 >>= 2
		temp0 += 7
		object.animationTimer++
		object.animationTimer %= 7
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	end switch
	switch object.value6
	case 1
		temp0 = object.value7
		temp0 >>= 2
		temp0 += 11
		object.value7++
		object.value7 &= 7
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 2
		temp0 = object.value7
		temp0 /= 3
		temp0 += 11
		object.value7++
		if object.value7 > 21
			object.value7 = 18
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	end switch
end event


event ObjectStartup
	if stage.playerListPos == 2
		if saveRAM[279] == 1
			LoadSpriteSheet("Global/Eggrobo.gif")
		else
			LoadSpriteSheet("Global/Eggman.gif")
		end if
	else
		LoadSpriteSheet("Global/Eggman.gif")
	end if
	SpriteFrame(-28, -32, 64, 56, 1, 1)
	SpriteFrame(-28, -32, 64, 56, 66, 1)
	SpriteFrame(-28, -32, 64, 56, 131, 1)
	SpriteFrame(-28, -32, 64, 56, 1, 58)
	SpriteFrame(-28, -32, 64, 56, 1, 1)
	SpriteFrame(-28, -32, 64, 56, 1, 58)
	SpriteFrame(-28, -32, 64, 56, 66, 58)
	SpriteFrame(-28, -32, 64, 56, 131, 58)
	SpriteFrame(-28, -32, 64, 56, 66, 1)
	SpriteFrame(-28, -32, 64, 56, 1, 115)
	SpriteFrame(-28, -32, 64, 56, 66, 115)
	SpriteFrame(34, 1, 10, 9, 196, 2)
	SpriteFrame(33, 0, 15, 11, 196, 12)
	SpriteFrame(34, -1, 22, 14, 213, 1)
	SpriteFrame(34, -8, 32, 26, 213, 16)
	SpriteFrame(34, -1, 22, 14, 213, 1)
	SpriteFrame(34, -8, 32, 26, 213, 16)
	SpriteFrame(34, 1, 10, 9, 196, 2)
	SpriteFrame(33, 0, 15, 11, 196, 12)
	foreach (TypeName[Eggman], arrayPos0, ALL_ENTITIES)
		if options.gameMode == 2
			temp0 = 7760
			temp0 <<= 16
			temp1 = 1512
			temp1 <<= 16
			ResetObjectEntity(arrayPos0, TypeName[Sign Post], 0, temp0, temp1)
			object[arrayPos0].frame = 1
			object[arrayPos0].value0 = 16
			object[arrayPos0].priority = PRIORITY_XBOUNDS
			object[arrayPos0].drawOrder = 2
		else
			temp0 = -0x700000
			temp0 += object[arrayPos0].xpos
			SetTableValue(temp0, 0, Eggman_table36)
			temp0 = 0x380000
			temp0 += object[arrayPos0].xpos
			SetTableValue(temp0, 1, Eggman_table36)
			temp0 = -0xC00000
			temp0 += object[arrayPos0].ypos
			SetTableValue(temp0, 2, Eggman_table36)
			temp0 = 0x600000
			temp0 += object[arrayPos0].xpos
			SetTableValue(temp0, 3, Eggman_table36)
			temp0 = -0x1000000
			temp0 += object[arrayPos0].ypos
			SetTableValue(temp0, 4, Eggman_table36)
			temp0 = -0x4C00000
			temp0 += object[arrayPos0].ypos
			SetTableValue(temp0, 5, Eggman_table36)
			temp0 = 0x13C0000
			temp0 += object[arrayPos0].xpos
			SetTableValue(temp0, 6, Eggman_table36)
			temp0 = -0x5000000
			temp0 += object[arrayPos0].ypos
			SetTableValue(temp0, 7, Eggman_table36)
			temp0 = 0xB80000
			temp0 += object[arrayPos0].xpos
			SetTableValue(temp0, 8, Eggman_table36)
			temp0 = -0x4D00000
			temp0 += object[arrayPos0].ypos
			SetTableValue(temp0, 9, Eggman_table36)
			object[arrayPos0].value0 = object[arrayPos0].xpos
			object[arrayPos0].value1 = object[arrayPos0].ypos
			object[arrayPos0].animation = 1
			object[arrayPos0].value8 = 8
		end if
	next
	switch stage.actNum
		case 2
			ResetObjectEntity(150, TypeName[Blank Object], 0, 0, 0)
			break
		case 3
			if options.gameMode == 2
				temp0 = 0
				while temp0 < 12
					GetTileLayerEntry(temp1, 0, 59, temp0)
					SetTileLayerEntry(temp1, 0, 60, temp0)
					SetTileLayerEntry(temp1, 0, 61, temp0)
					SetTileLayerEntry(temp1, 0, 62, temp0)
					SetTileLayerEntry(temp1, 0, 63, temp0)
					temp0++
				loop
			end if
			break
	end switch
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
